/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved.
 *
 */

import { InputFile, OutputFile } from '@ohos/hvigor';
import path from 'path';

import { TaskNames } from '../common/task-names.js';
import { BuildArtifactConst } from '../../const/build-directory-const.js';
import { TargetTaskService } from '../service/target-task-service.js';
import { OhosModuleTask } from '../task/ohos-module-task.js';
import { InjectUtil } from '../../utils/inject-util.js';
import { overWriteSourceMap } from '../../utils/sourcemap-util.js';

/**
 * 覆盖率场景下替换sourceMap
 *
 * @since 2024-07-25
 */
export class ReplaceSourcemapInCoverageMode extends OhosModuleTask {
  @InputFile() unitTestSourceMapPath = path.join(this.pathInfo.getModulePath(),
    '.test/default/intermediates/test/coverage_sourcemap/unitTestSourceMap.map');
  @OutputFile() defaultSourceMapPath: string = path.join(this.pathInfo.getInterMediatesAssetsPath(), 'ets', BuildArtifactConst.SOURCEMAPS_MAP);

  constructor(taskService: TargetTaskService) {
    super(taskService, TaskNames.Task.REPLACE_SOURCEMAP_IN_COVERAGE_MODE);
  }

  protected async doTaskAction(): Promise<void> {
    if (!InjectUtil.isOhosTestCoverage()) {
      return;
    }

    await overWriteSourceMap(this.defaultSourceMapPath, this.unitTestSourceMapPath);
  }

  initTaskDepends(): void {
    // do nothing
  }
}